# -*- coding: utf-8 -*-
"""常用模块"""


def str_hex():
    """十六进制"""
    import binascii
    key_bytes = b'0123456789abcdef'
    key_str = "0123456789abcdef"

    hex1 = binascii.a2b_hex(key_str)
    print(hex1)
    hex2 = binascii.b2a_hex(hex1)
    print(hex2)

    hex3 = binascii.hexlify(key_bytes)  # 推荐使用
    print(hex3)
    hex4 = binascii.unhexlify(hex3)
    print(hex4)

    hex5 = hex(9)  # 仅能转换整数
    print(hex5)
    hex6 = ''.join([("%x" % x).zfill(2) for x in key_bytes])
    print(hex6)

    pass


def md5():
    """MD5"""
    # hashlib是自带的模块
    import hashlib

    data = '0123456789abcdef'
    if isinstance(data, str):
        return hashlib.md5(data.encode('utf-8')).hexdigest()
    elif isinstance(data, bytes):
        return hashlib.md5(data).hexdigest()
    pass


def base64():
    """base64：明文和密文都是bytes类型"""
    import base64
    data = b'0123456789abcdef'
    # data = '0123456789abcdef'.encode()
    s1 = base64.b64encode(data)
    print(s1)
    b1 = base64.b64decode(s1)
    print(b1)
    import binascii
    print(binascii.hexlify(b1))
    pass


if __name__ == '__main__':
    # md5()
    base64()
    pass
